﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Scroll
{
    class ScrollTile : DrawableGameComponent //herança com a classe DrawableGameComponent
    {
        private Texture2D tile;
        private Vector2[] vPosicao;
        private Point dimensao;
        private int qtdTiles;
        private float velocidade;


        public ScrollTile(Texture2D tile, Point dimensao, int qtdTiles, float velocidade, Game1 game):base(game)
        {
            this.tile = tile;
            this.dimensao = dimensao;
            this.qtdTiles = qtdTiles;
            this.velocidade = velocidade;
            Initialize();
        }

        public void Initialize()
        {
            Random rnd = new Random();

            vPosicao = new Vector2[qtdTiles];

            for (int i = 0; i < qtdTiles; i++)
            {
                int x = rnd.Next(0, dimensao.X);
                int y = rnd.Next(0, dimensao.Y);

                vPosicao[i] = new Vector2(x,y);
            }        

        }    

        public void Update(GameTime gameTime)
        {
            for (int i = 0; i < qtdTiles; i++)
            {
                vPosicao[i].Y += (float)gameTime.ElapsedGameTime.TotalMilliseconds * velocidade;

                if(vPosicao[i].Y > dimensao.Y)
                {
                    vPosicao[i].Y = 0 - tile.Height;
                }
            }
        }

        public void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;

            //spriteBatch.Begin();

            for (int i = 0; i < qtdTiles; i++)
            {
                spriteBatch.Draw(tile, vPosicao[i], Color.White);
            }

           // spriteBatch.End();
        }
    
    }
}
